package com.pkq.two;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParentMatchChild {


    // 1. 通过正则表达式匹配
    public boolean isPattern(String p, String c) {
        int i = 0;

        Pattern pattern = Pattern.compile(c);
        Matcher matcher = pattern.matcher(p);
        while (matcher.find()) {
            System.out.println("matcher.group() = " + matcher.group());
            System.out.println("++i = " + ++i);
        }

        return pattern.matcher(p).find();
    }

/*    public boolean isPatternn(String p, String c) {
        return Pattern.compile(c).matcher(p).find();
    }*/


    // 2. 通过split()方法找 ： 1）父、子串长度不相同,分割出的数组大于1   2）父、子串长度不相同,分割出的数组等于1   3）父、子串长度不相同
    public boolean isPattern2(String p, String c) {
        String[] split = p.split(c);

        if (p.length() != c.length() && split.length > 1) {
            return true;
        } else if (p.length() != c.length() && split.length == 1) {
            return p.startsWith(c, p.length() - c.length());
        } else {
            return p.matches(c);
        }
    }
}
